package com.zcxy.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.google.common.base.Preconditions;
import com.zcxy.anotation.Log;
import com.zcxy.core.bean.PageQuery;
import com.zcxy.core.bean.Response;
import com.zcxy.sys.entity.SysDict;
import com.zcxy.sys.entity.SysDictItem;
import com.zcxy.sys.mapper.SysDictItemMapper;
import com.zcxy.sys.pojo.SysDictDto;
import com.zcxy.sys.pojo.SysDictItemVo;
import com.zcxy.sys.pojo.SysDictVo;
import com.zcxy.sys.service.ISysDictItemService;
import com.zcxy.sys.service.ISysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * @author zcc
 * @version 1.0.0
 * @ClassName SysDictController.java
 * @Description 字典主表
 * @createTime 2020-06-04
 */
@RestController
@RequestMapping("/api/sysDict")
@Api(tags = "<font color='red'>字典管理</font>")
@Slf4j
@JsonInclude(JsonInclude.Include.NON_NULL)
public class SysDictController {
    @Autowired
    private ISysDictService iSysDictService;
    @Autowired
    private ISysDictItemService iSysDictItemService;
    @Autowired
    private SysDictItemMapper sysDictItemMapper;

    @Log(value = "获取(字典主表)分页数据列表")
    @PostMapping(value = "/queryPageList")
    @ApiOperation(value = "获取(字典主表)分页数据列表", notes = "获取(字典主表)分页数据列表")
    public Response<IPage<SysDictVo>> queryPageList(@RequestBody PageQuery<SysDict> queryVo) {
        return Response.ok(iSysDictService.queryPageList(queryVo));
    }

    @Log(value = "获取(字典主表)分页数据列表")
    @PostMapping(value = "/queryList")
    @ApiOperation(value = "获取(字典主表)数据列表", notes = "获取(字典主表)数据列表")
    public Response<List<SysDictVo>> queryList(@RequestBody SysDict queryVo) {
        return Response.ok(iSysDictService.queryList(queryVo));
    }


    @Log(value = "新增(字典主表)数据")
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增(字典主表)数据", notes = "新增(字典主表)数据")
    public Response<SysDict> addSysDict(@RequestBody @Validated SysDict queryVo) {

        return Response.ok(iSysDictService.addSysDict(queryVo));
    }

    @Log(value = "修改(字典主表)数据")
    @PutMapping(value = "/update")
    @ApiOperation(value = "修改(字典主表)数据", notes = "修改(字典主表)数据")
    public Response updateSysDict(@RequestBody @Validated SysDict queryVo) {
        iSysDictService.updateSysDict(queryVo);
        return Response.ok();
    }


    @Log(value = "删除(字典主表)数据")
    @DeleteMapping(value = "/dels")
    @ApiOperation(value = "删除(字典主表)数据", notes = "删除(字典主表)数据")
    public Response<List<String>> removeSysDicts(@RequestBody List<String> ids) {

        return Response.ok(iSysDictService.removeAllSysDict(ids));
    }

    @Log("启用主表表用户数据")
    @PutMapping(value = "/updateEnable")
    @ApiOperation(value = "启用主表用户数据", notes = "启用主表用户数据")
    public Response updateEnable(@RequestBody List<String> ids) {
        iSysDictService.updateEnable(ids);
        return Response.ok();
    }

    @Log("禁用主表用户数据")
    @PutMapping(value = "/updateDisableEnable")
    @ApiOperation(value = "禁用主表用户数据", notes = "禁用主表用户数据")
    public Response updateDisableEnable(@RequestBody List<String> ids) {
        iSysDictService.updateDisable(ids);
        return Response.ok();
    }

    @Log(value = "查看(字典主表)")
    @GetMapping(value = "/view")
    @ApiOperation(value = "查看(字典主表)数据", notes = "查看(字典主表)数据")
    public Response<SysDictVo> viewSysDict(@RequestParam("id") String id) {
        return Response.ok(iSysDictService.viewSysDict(id));
    }

    @Log(value = "查看(字典子表)")
    @GetMapping(value = "/viewDictItem")
    @ApiOperation(value = "查看(字典子表)数据", notes = "查看(字典子表)数据")
    public Response<SysDictItemVo> viewSysDictItem(@RequestParam("id") String id) {
        return Response.ok(iSysDictItemService.viewSysDictItem(id));
    }

    @Log("导出字典主表")
    @ApiOperation("导出字典主表")
    @PostMapping(value = "/download")
    public void download(HttpServletResponse response, @RequestBody SysDict queryVo) throws IOException {
        iSysDictService.download(queryVo, response);
    }

    @Log(value = "新增(字典子表)数据")
    @PostMapping(value = "/addtable")
    @ApiOperation(value = "新增(字典子表)数据", notes = "新增(字典子表)数据")
    public Response addSysDictItem(@RequestBody @Validated SysDictItem queryVo) {

        Preconditions.checkNotNull(queryVo, "当前参数为空");
        Preconditions.checkNotNull(queryVo.getDictId(), "dictId 不能为空");
        Preconditions.checkArgument(!queryVo.getDictId().equals(""), "dictId 不能为空字符串");

        iSysDictItemService.addSysDictItem(queryVo);
        return Response.ok();
    }

    @Log(value = "修改(字典子表)数据")
    @PutMapping(value = "/updatetable")
    @ApiOperation(value = "修改(字典子表)数据", notes = "修改(字典子表)数据")
    public Response updateSysDictItem(@RequestBody @Validated SysDictItem queryVo) {
        Preconditions.checkNotNull(queryVo, "当前参数为空");
        Preconditions.checkNotNull(queryVo.getDictId(), "dictId 不能为空");
        Preconditions.checkArgument(!queryVo.getDictId().equals(""), "dictId 不能为空字符串");
        iSysDictItemService.updateSysDictItem(queryVo);
        return Response.ok();

    }


    @Log(value = "删除(字典子表)数据")
    @DeleteMapping(value = "/delstable")
    @ApiOperation(value = "删除(字典子表)数据", notes = "删除(字典子表)数据")
    public Response removeAllSysDictItem(@RequestBody List<String> ids) {
        iSysDictItemService.removeAllSysDictItem(ids);
        return Response.ok();
    }

    @Log("启用字典子表用户数据")
    @PutMapping(value = "/updateEnabletable")
    @ApiOperation(value = "启用子表用户数据", notes = "启用子表用户数据")
    public Response updateEnabletable(@RequestBody List<String> ids) {
        iSysDictItemService.updateEnable(ids);
        return Response.ok();
    }

    @Log("禁用子表用户数据")
    @PutMapping(value = "/updateDisableEnabletable")
    @ApiOperation(value = "禁用子表用户数据", notes = "禁用子表用户数据")
    public Response updateDisableEnabletable(@RequestBody List<String> ids) {
        iSysDictItemService.updateDisable(ids);
        return Response.ok();
    }

    @Log(value = "根据主表id查看(字典子表)")
    @GetMapping(value = "/viewtable")
    @ApiOperation(value = "根据主表id查看(字典子表)数据", notes = "根据主表id查看(字典子表)数据")
    public List<SysDictItem> viewSysDictItemsById(@RequestParam("id") String id) {
        return iSysDictItemService.viewSysDictItemsById(id);
    }


    @Log("导出字典子表")
    @ApiOperation("导出字典子表")
    @PostMapping(value = "/downloadtable")
    public void download(HttpServletResponse response, @RequestBody SysDictItem queryVo) throws IOException {
        iSysDictItemService.download(queryVo, response);
    }


    @Log(value = "批量添加")
    @PostMapping(value = "/adds")
    @ApiOperation(value = "批量添加", notes = "批量添加")
    public void addSysDictItems(@RequestBody SysDictDto dto) {
        iSysDictItemService.addSysDictItems(dto.getText(), dto.getId());
    }


    @Log(value = "根据列表code获取列表值的下拉数据")
    @PostMapping(value = "/queryListSelectData")
    @ApiOperation(value = "根据列表code获取列表值的下拉数据", notes = "根据列表code获取列表值的下拉数据")
    public Response<List<SysDictItem>> queryListSelectDataByCode(String code) {
        return Response.ok(iSysDictService.queryListSelectDataByCode(code));
    }


}
